package com.hnou.boxuegu.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.widget.ImageView;

public class UtilsHelper {
    /*
    判断当前用户是否存在
     */
    public static boolean isExistUserName(Context context, String userName){
        boolean has_userName = false;
        SharedPreferences sp = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        // tom:123
        String spPsw = sp.getString(userName, ""); // 根据用户名去查找密码，如果找到了就表示存在，否则不存在
        if(!TextUtils.isEmpty(spPsw)){
            // 存在
            has_userName = true;
        }
        return  has_userName;
    }
    /**
     * 保存用户名和密码
     */
    public static void saveUserInfo(Context context, String userName, String password){
        String md5Psw = MD5Utils.md5(password);
        SharedPreferences sp= context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);

        SharedPreferences.Editor edit = sp.edit();
        edit.putString(userName,md5Psw); // 保存到sp中，键 为 username，值 为 md5Psw
        edit.commit(); // 提交保存操作
    }
    public static String readPsw(Context context,String userName){
        SharedPreferences sp=context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        String spPsw=sp.getString(userName,"");
        return spPsw;
    }
    //保存登录状态和登录用户名
    public static void saveLoginStatus(Context context,boolean status,String userName){
        SharedPreferences sp= context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putBoolean("isLogin",status);
        editor.putString("loginUserName",userName);
        editor.commit();
    }
    public static boolean readLoginStatus(Context context){
        SharedPreferences sp= context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        boolean isLogin=sp.getBoolean("isLogin",false);
        return isLogin;
    }
    public static void clearLoginStatus(Context context){
        SharedPreferences sp= context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putBoolean("isLogin",false);
        editor.putString("loginUserMane","");
        editor.commit();
    }
    public static String readloginUserName(Context context){
        SharedPreferences sp=context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        String userName=sp.getString("loginUserName","");
        return userName;
    }
    public static  void setABCDEnable(boolean value, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d){
        iv_a.setEnabled(value);
        iv_b.setEnabled(value);
        iv_c.setEnabled(value);
        iv_d.setEnabled(value);
    }
}

